package org.example.consumer;

import com.rabbitmq.client.*;
import org.example.constant.Constants;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Consumer1 {
    public static void main(String[] args) throws IOException, TimeoutException {
        //建立连接
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("123.57.1.114");
        factory.setPort(5672);
        factory.setVirtualHost("test");
        factory.setUsername("admin");
        factory.setPassword("admin");

        Connection connection = factory.newConnection();
        //开启信道
        Channel channel = connection.createChannel();
        //消费消息
        Consumer consumer = new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("接收到消息：" + new String(body));
//                channel.basicReject(envelope.getDeliveryTag(), false);
                channel.basicNack(envelope.getDeliveryTag(), true,false);
            }
        };
        channel.basicConsume(Constants.CONFIRM_QUEUE,false,consumer);
//        channel.close();
//        connection.close();
    }
}
